package cn.iocoder.yudao.module.system.controller.admin.pushManage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.pushManage.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.pushManage.PushManageDO;
import cn.iocoder.yudao.module.system.convert.pushManage.PushManageConvert;
import cn.iocoder.yudao.module.system.service.pushManage.PushManageService;

@Api(tags = "管理后台 - 推送管理")
@RestController
@RequestMapping("/system/push-manage")
@Validated
public class PushManageController {

    @Resource
    private PushManageService pushManageService;

    @PostMapping("/create")
    @ApiOperation("创建推送管理")
    @PreAuthorize("@ss.hasPermission('system:push-manage:create')")
    public CommonResult<Long> createPushManage(@Valid @RequestBody PushManageCreateReqVO createReqVO) {
        Long id = pushManageService.createPushManage(createReqVO);
        return success(id);
    }

    @PutMapping("/update")
    @ApiOperation("更新推送管理")
    @PreAuthorize("@ss.hasPermission('system:push-manage:update')")
    public CommonResult<Boolean> updatePushManage(@Valid @RequestBody PushManageUpdateReqVO updateReqVO) {
        pushManageService.updatePushManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除推送管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:push-manage:delete')")
    public CommonResult<Boolean> deletePushManage(@RequestParam("id") Long id) {
        pushManageService.deletePushManage(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得推送管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:push-manage:query')")
    public CommonResult<PushManageRespVO> getPushManage(@RequestParam("id") Long id) {
        PushManageDO pushManage = pushManageService.getPushManage(id);
        return success(PushManageConvert.INSTANCE.convert(pushManage));
    }

    @GetMapping("/list")
    @ApiOperation("获得推送管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:push-manage:query')")
    public CommonResult<List<PushManageRespVO>> getPushManageList(@RequestParam("ids") Collection<Long> ids) {
        List<PushManageDO> list = pushManageService.getPushManageList(ids);
        return success(PushManageConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得推送管理分页")
    @PreAuthorize("@ss.hasPermission('system:push-manage:query')")
    public CommonResult<PageResult<PushManageRespVO>> getPushManagePage(@RequestBody PushManagePageReqVO pageVO) {
        PageResult<PushManageDO> pageResult = pushManageService.getPushManagePage(pageVO);
        return success(PushManageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出推送管理 Excel")
    @PreAuthorize("@ss.hasPermission('system:push-manage:export')")
    @OperateLog(type = EXPORT)
    public void exportPushManageExcel(@Valid PushManageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PushManageDO> list = pushManageService.getPushManageList(exportReqVO);
        // 导出 Excel
        List<PushManageExcelVO> datas = PushManageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "推送管理.xls", "数据", PushManageExcelVO.class, datas);
    }

    @GetMapping("/nowPushMessage")
    @ApiOperation("立即推送")
    @PreAuthorize("@ss.hasPermission('system:push-manage:create')")
    public CommonResult<Long> nowPushMessage(@RequestParam("id") Long id) {
        PushManageDO pushManageDO=new PushManageDO();
        pushManageDO.setId(id);
        pushManageDO.setOldPushTime(new Date());
        pushManageDO.setStatus("1");
        pushManageDO.setActionNum(1);
        pushManageService.updatePushManage(pushManageDO);
        return success(pushManageService.nowPushMessage(id));
    }

    @GetMapping("/pushMessage")
    @ApiOperation("定时推送")
    public CommonResult<Long> pushMessage(@RequestParam("id") Long id) {
        return success(pushManageService.pushMessage(id));
    }
}
